package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.CommVLayoutAdapter;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.BrandSeriesInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.mvp.mine.ChooseCarModelSeriesCovenant;
import com.chexiehui.jc.mvp.mine.ChooseCarModelSeriesPresenter;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.widget.LoadingLayout;
import com.nex3z.flowlayout.FlowLayout;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 选择车型
 */
public class ChooseCarModelSeriesFragment extends BaseMvpFragment<ChooseCarModelSeriesPresenter> implements ChooseCarModelSeriesCovenant.View, View.OnClickListener {

    @BindView(R.id.iv_brand_image)
    ImageView ivBrandImage;
    @BindView(R.id.tv_brand_name)
    TextView tvBrandName;
    @BindView(R.id.fl_follow_tip)
    FlowLayout flFollowTip;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    private DelegateAdapter listAdapter;
    private CommVLayoutAdapter<CarSeriesInfo> saleListAdapter;
    private BrandInfo brandInfo;
    private String carType = "";
    private List<DictInfo> carTypes = new ArrayList<>();
    private int carTypePosition = -1;
    private OnSeriesChooseListener onSeriesChooseListener;

    public static ChooseCarModelSeriesFragment getInstance(BrandInfo brandInfo) {
        ChooseCarModelSeriesFragment fragment = new ChooseCarModelSeriesFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", brandInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void beforeSetView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            brandInfo = (BrandInfo) bundle.get("entity");
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_car_model_series;
    }

    @Override
    protected void initView(Bundle var1) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(mActivity);
        mRecycler.setLayoutManager(virtualLayoutManager);
        ((SimpleItemAnimator) mRecycler.getItemAnimator()).setSupportsChangeAnimations(false);
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        mRecycler.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        listAdapter = new DelegateAdapter(virtualLayoutManager, false);
        mRecycler.setAdapter(listAdapter);
        int dp_10 = SizeUtils.dp2px(10);
        LinearLayoutHelper helper = new LinearLayoutHelper(SizeUtils.dp2px(1));
        helper.setMargin(0, dp_10, 0, dp_10);
        helper.setBgColor(ContextCompat.getColor(mActivity, R.color.colorTransparent));
        saleListAdapter = new CommVLayoutAdapter<CarSeriesInfo>(mActivity, new ArrayList<>(),
                R.layout.item_choose_car_mode_seariesl, helper) {
            @Override
            public void convert(ViewHolder holder, int position, CarSeriesInfo item) {
                GlideUtil.loadImageViewCrop(mActivity, item.getImg(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_square, R.drawable.shape_square);
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_guidance_price, item.getGuidancePriceMaxMinStr());
            }
        };
        saleListAdapter.setItemClickListener((view, position, item) -> {
            if(onSeriesChooseListener != null){
                onSeriesChooseListener.onSeriesChoose(item);
            }
        });
        listAdapter.addAdapter(saleListAdapter);
        loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                if (brandInfo != null) {
                    loadingLayout.setStatus(LoadingLayout.Loading);
                    mvpPresenter.getBrandSeriesInfo();
                }else{
                    loadingLayout.setStatus(LoadingLayout.Empty);
                    loadingLayout.setEmptyText("没有找到任何数据");
                }
            }
        });
        if (brandInfo == null) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
            return;
        }
        GlideUtil.loadImageViewCrop(mActivity, brandInfo.getImg(), ivBrandImage,
                R.drawable.shape_square, R.drawable.shape_square);
        tvBrandName.setText(brandInfo.getName());
        mvpPresenter.getBrandSeriesInfo();
    }

    @Override
    protected ChooseCarModelSeriesPresenter createPresenter() {
        return new ChooseCarModelSeriesPresenter(this);
    }

    @Override
    public void onGetBrandSeriesInfoSuccess(BaseModel<BrandSeriesInfo> successResult) {
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (carTypes.size() <= 0) {
            carTypes.addAll(successResult.getData().getCarTypeList());
            setCarTypeFlowTag(0);
        }
        saleListAdapter.getData().clear();
        saleListAdapter.getData().addAll(successResult.getData().getSeriesList());
        saleListAdapter.notifyDataSetChanged();
    }

    private void setCarTypeFlowTag(int position) {
        if (carTypePosition != position) {
            carTypePosition = position;
            carType = carTypes.get(position).getKey();
            flFollowTip.removeAllViews();
            for (int i = 0; i < carTypes.size(); i++) {
                RadiusTextView radiusTextView = (RadiusTextView) View.inflate(mActivity, R.layout.item_only_car_type, null);
                radiusTextView.setTag(i);
                radiusTextView.setText(carTypes.get(i).getValue());
                radiusTextView.setOnClickListener(this);
                radiusTextView.setSelected(carTypePosition == i);
                flFollowTip.addView(radiusTextView);
            }
        }
    }

    @Override
    public void onGetBrandSeriesInfoFailure(BaseModel<Object> failureResult) {
        if (carTypes.size() <= 0) {
            if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
                loadingLayout.setStatus(LoadingLayout.Empty);
                loadingLayout.setEmptyText(failureResult.getResultInfo());
            } else {
                loadingLayout.setStatus(LoadingLayout.Error);
                loadingLayout.setErrorText(failureResult.getResultInfo());
            }
        }
    }

    @Override
    public String getBrandId() {
        return brandInfo.getId();
    }

    @Override
    public String getCarType() {
        return carType;
    }

    @Override
    public void onClick(View v) {
        Integer position = (Integer) v.getTag();
        setCarTypeFlowTag(position);
        mvpPresenter.getBrandSeriesInfo();
    }

    public void setOnChooseListener(OnSeriesChooseListener onSeriesChooseListener) {
        this.onSeriesChooseListener = onSeriesChooseListener;
    }

    public interface OnSeriesChooseListener{
        void onSeriesChoose(CarSeriesInfo carSeriesInfo);
    }

}
